package la.iok.hzsvn.lewin.movie.cloud.controller;

import la.iok.hzsvn.lewin.cloud.invoker.service.RestServiceInvoker;
import la.iok.hzsvn.lewin.movie.cloud.entity.Device;
import la.iok.hzsvn.lewin.movie.cloud.model.DeviceParam;
import la.iok.hzsvn.lewin.movie.cloud.model.SimpleDeviceParam;
import la.iok.hzsvn.lewin.movie.cloud.model.TokenInfo;
import la.iok.hzsvn.lewin.movie.cloud.service.DashBoardService;
import la.iok.hzsvn.lewin.movie.cloud.service.DeviceService;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.DeviceVo;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("devices")
public class DeviceController {
    private final DeviceService deviceService;
    private final RestServiceInvoker restServiceInvoker;
    private final DashBoardService dashBoardService;

    public DeviceController(DeviceService deviceService,
                            RestServiceInvoker restServiceInvoker,
                            DashBoardService dashBoardService) {
        this.deviceService = deviceService;
        this.restServiceInvoker = restServiceInvoker;
        this.dashBoardService = dashBoardService;
    }

    @PostMapping
    public OperatorResponse save(@RequestHeader Token token, @RequestBody DeviceParam deviceParam){
        deviceService.qrCodeSave(token,deviceParam);
        return OperatorResponses.success();
    }
    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id){
        deviceService.delete(token,id);
        return OperatorResponses.success();
    }
    @PutMapping("{id}")
    public OperatorResponse modify(@RequestHeader Token token, @PathVariable Long id,@RequestBody DeviceParam deviceParam){
        deviceService.update(token,id,deviceParam.getName(),deviceParam.getRoomId(),deviceParam.getOrgUnitId());
        return OperatorResponses.success();
    }

    @GetMapping
    public SingleDataResponse<PageVo<DeviceVo>> page(@RequestHeader Token token, int page, int limit, Long orgUnitId, Long roomId, String name){
        page = Math.max(1,page);
        limit = Math.min(Math.max(1,limit),50);
        PageVo<DeviceVo> pageRecords=deviceService.page(page,limit,token,orgUnitId,roomId,name);
        return new SingleDataResponse<>(pageRecords);
    }
    @PostMapping("token")
    public SingleDataResponse<TokenInfo> token(@RequestBody SimpleDeviceParam deviceParam){
        Device device = deviceService.select(deviceParam.getCode());
        ErrorCode.DEVICE_NOT_EXIST.assertNotNull(device,deviceParam.getCode());
        Map<String,String> params = new HashMap<>();
        params.put("deviceId",String.valueOf(device.getId()));
        params.put("orgUnitId",String.valueOf(device.getOrgUnitId()));
        params.put("roomId",device.getRoomId() != null && device.getRoomId() > 0?String.valueOf(device.getRoomId()):"");
        params.put("code",device.getCode());
        params.put("grantType","direct_token");
        params.put("clientId","lewin");
        params.put("clientSecret","lwp2wd");
        TokenInfo token = restServiceInvoker.postForObject("authserver",
                "/oauth/token?grant_type={grantType}&client_id={clientId}&client_secret={clientSecret}&deviceId={deviceId}&orgUnitId={orgUnitId}&roomId={roomId}&code={code}",
                null, TokenInfo.class,params);
        return new SingleDataResponse<>(token);
    }
    @GetMapping("total")
    public SingleDataResponse<Integer> total(@RequestHeader Token token, Long orgUnitId ){
        return new SingleDataResponse<>(dashBoardService.deviceTotalCount(token,orgUnitId));
    }

    @GetMapping("info")
    public SingleDataResponse<DeviceVo> info(@RequestHeader Token token){
        return new SingleDataResponse<>(deviceService.po2vo(deviceService.select(token.getDeviceId())));
    }
}
